package com.gdpu.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdpu.bean.Count;
import com.gdpu.bean.Goods;
import com.gdpu.bean.OrderForm;
import com.gdpu.common.DataGridView;
import com.gdpu.common.MapNode;
import com.gdpu.common.MapNodeHead;
import com.gdpu.common.ResultObj;
import com.gdpu.service.CountService;
import com.gdpu.service.GoodsService;
import com.gdpu.service.OrderFormService;
import com.gdpu.service.ShopService;
import com.gdpu.vo.OrderFormVo;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hjs
 * @since 2021-02-14
 */
@RestController
@RequestMapping("/orderForm")
public class OrderFormController {
    @Resource
    private OrderFormService orderFormService;

    @Resource
    private CountService countService;

    @Resource
    private ShopService shopService;

    @Resource
    private GoodsService goodsService;

    //后台处获取所有订单
    @RequestMapping("loadAllOrder")
    public DataGridView loadAllOrder(OrderFormVo orderFormVo){
        IPage<OrderForm> page = new Page<>(orderFormVo.getPage(),orderFormVo.getLimit());
        QueryWrapper<OrderForm> queryWrapper = new QueryWrapper<>();
        orderFormService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    //小程序处获取所有订单
    @RequestMapping("loadOrderForm")
    public DataGridView loadOrderForm(OrderFormVo orderFormVo){
        IPage<OrderForm> page = new Page<>(orderFormVo.getPage(),orderFormVo.getLimit());
        QueryWrapper<OrderForm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(null !=orderFormVo.getOpenId(),"open_id",orderFormVo.getOpenId());
        List<OrderForm> list = orderFormService.list(queryWrapper);
//        list.
        List<MapNodeHead> map = new ArrayList<>();
        Hashtable<Integer,Integer> ids = new Hashtable<>();
        for(OrderForm orderForm : list){
            if (!ids.containsKey(orderForm.getOrderId())){

                MapNodeHead mapNodeHead = new MapNodeHead(orderForm.getOrderId(), orderForm.getPriceSum(), shopService.getById(orderForm.getShopId()).getAddress(), orderForm.getOrderTime(), orderForm.getPhone(),orderForm.getFinish(),orderForm.getOpenId());
                map.add(mapNodeHead);
                int value = map.indexOf(mapNodeHead);
                ids.put(orderForm.getOrderId(),value);
                int index =ids.get(orderForm.getOrderId());
                Goods goods = goodsService.getById(orderForm.getGoodsId());
                String goodsname = goods.getName();
                String icon = goods.getImageUrl();
                Integer number = orderForm.getNumber();
                Integer price = orderForm.getPriceSum();
                MapNode mapNode = new MapNode(goodsname,icon,number,price);
                map.get(index).getChildren().add(mapNode);
            }else {
                int index =ids.get(orderForm.getOrderId());
                Goods goods = goodsService.getById(orderForm.getGoodsId());
                String goodsname = goods.getName();
                String icon = goods.getImageUrl();
                Integer number = orderForm.getNumber();
                Integer price = orderForm.getPriceSum();
                MapNode mapNode = new MapNode(goodsname,icon,number,price);

                map.get(index).getChildren().add(mapNode);
            }
        }

        return new DataGridView(map);
     }

    //下单，记录到数据库中
    @RequestMapping("getForm")
    public void getForm(OrderFormVo orderFormVo){
//        System.out.println("OpenId = " + OpenId);
//        System.out.println(orderFormVo.getPriceSum());
        Count count = countService.getById(1);
        count.setOrderId(count.getOrderId()+1);
        Integer orderid =  count.getOrderId();
        countService.updateById(count);
        //将字符串转化成标准josn格式
        String jsons = "{\"data\":" +orderFormVo.getBought()+"}";
//        System.out.println("jsons = " + jsons);
        JSONObject json = new JSONObject(jsons);
        JSONArray array = json.getJSONArray("data");
        String[] str2 = orderFormVo.getNumbers().substring(1,orderFormVo.getNumbers().length()-1).split(",");
        for(int i =0;i<array.size();i++){
//            String title = array.getJSONObject(i).get("title").toString();
//            System.out.println("title = " + title);
            String id = array.getJSONObject(i).get("id").toString();
//            System.out.println("id = " + id);
            orderFormVo.setOrderId(orderid);
            orderFormVo.setOrderTime(new Date());
            orderFormVo.setNumber(Integer.parseInt(str2[Integer.parseInt(id)-9]));
            orderFormVo.setGoodsId(Integer.parseInt(id)+2);
            orderFormVo.setFinish(0);
            orderFormService.save(orderFormVo);
        }
//        for (int i =0;i<str2.length;i++){
//            System.out.println(str2[i]);
//        }
    }


    @RequestMapping("orderFinish")
    public ResultObj orderFinish(OrderFormVo orderFormVo){
        try{
            orderFormVo.setFinish(1);
            QueryWrapper<OrderForm> queryWrapper = new QueryWrapper<>();
//            System.out.println("-----------------------------"+orderFormVo.getOrderId());
            queryWrapper.eq("order_id",orderFormVo.getOrderId());
            orderFormService.update(orderFormVo,queryWrapper);

            return ResultObj.FINISH_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.FINISH_ERROR;
        }
    }
}

